package com.example.back.service;

import com.example.back.entity.Notice;
import com.example.back.entity.otd.NoticeCommentRelation;
import com.example.back.mapper.NoticeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;

    public int addNotice(Notice notice){
        return noticeMapper.addNotice(notice);
    }

    public int deleteNotice(Serializable id){
        return noticeMapper.deleteNotice(id);
    }

    public int updateNotice(Notice notice){
        return noticeMapper.updateNotice(notice);
    }

    public Notice selectNoticeById(Serializable id){
        return noticeMapper.selectNoticeById(id);
    }

    public ArrayList<Notice> selectNotice(Notice notice){
        return noticeMapper.selectNotice(notice);
    }

    public int markNoticeAsRead(Serializable id) {
        return noticeMapper.markNoticeAsRead(id);
    }

    public NoticeCommentRelation selectNoticeCommentRelationById(Serializable noticeId) {
        NoticeCommentRelation relation = noticeMapper.selectNoticeCommentRelationById(noticeId);
        if (relation == null) {
            // 返回一个空对象或抛出适当的异常
            relation = new NoticeCommentRelation();
            relation.setNoticeId((Integer) noticeId);
            relation.setNoticeType("comment");
            relation.setNoticeText("评论通知");
            relation.setCommentText("评论内容已删除");
        }
        return relation;
    }

    /**
     * 获取用户的未读通知总数
     */
    public Map<String, Integer> getUnreadNoticeCount(Integer userId) {
        Map<String, Integer> result = new HashMap<>();
        int count = noticeMapper.countUnreadNotices(userId);
        result.put("count", count);
        return result;
    }

    /**
     * 获取用户各类通知的未读计数
     */
    public Map<String, Integer> getUnreadNoticeCounts(Integer userId) {
        Map<String, Integer> result = new HashMap<>();

        // 获取所有类型的未读计数
        int allCount = noticeMapper.countUnreadNotices(userId);
        int likeCount = noticeMapper.countUnreadNoticesByType(userId, "like");
        int commentCount = noticeMapper.countUnreadNoticesByType(userId, "comment");
        int collectionCount = noticeMapper.countUnreadNoticesByType(userId, "collection");
        int followCount = noticeMapper.countUnreadNoticesByType(userId, "follow");
        int messageCount = noticeMapper.countUnreadNoticesByType(userId, "message");

        result.put("all", allCount);
        result.put("like", likeCount);
        result.put("comment", commentCount);
        result.put("collection", collectionCount);
        result.put("follow", followCount);
        result.put("message", messageCount);

        return result;
    }
}
